package com.ruoyi.electricity.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.electricity.domain.DTO.ElectricityByArea;
import com.ruoyi.electricity.domain.DTO.RunStatusDeviceDetection;
import com.ruoyi.electricity.domain.ElectricityData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.electricity.mapper.TElectricityDataMapper;
import com.ruoyi.electricity.domain.TElectricityData;
import com.ruoyi.electricity.service.ITElectricityDataService;

/**
 * 用电Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-28
 */
@Service
public class TElectricityDataServiceImpl implements ITElectricityDataService {
    @Autowired
    private TElectricityDataMapper tElectricityDataMapper;

    /**
     * 查询用电
     *
     * @param id 用电ID
     * @return 用电
     */
    @Override
    public TElectricityData selectTElectricityDataById(String id) {
        return tElectricityDataMapper.selectTElectricityDataById(id);
    }

    /**
     * 查询用电列表
     *
     * @param tElectricityData 用电
     * @return 用电
     */
    @Override
    public List<TElectricityData> selectTElectricityDataList(TElectricityData tElectricityData) {
        return tElectricityDataMapper.selectTElectricityDataList(tElectricityData);
    }

    /**
     * 新增用电
     *
     * @param tElectricityData 用电
     * @return 结果
     */
    @Override
    public int insertTElectricityData(TElectricityData tElectricityData) {
        return tElectricityDataMapper.insertTElectricityData(tElectricityData);
    }

    /**
     * 修改用电
     *
     * @param tElectricityData 用电
     * @return 结果
     */
    @Override
    public int updateTElectricityData(TElectricityData tElectricityData) {
        return tElectricityDataMapper.updateTElectricityData(tElectricityData);
    }

    @Override
    public List<ElectricityByArea> getElectricityByAreaWithinTime(Date startTime, Date endTime) {
        // 获取所有区域名称
        List<String> areaList = tElectricityDataMapper.selectAllArea();
        List<ElectricityByArea> electricityByAreaList = tElectricityDataMapper.getElectricityByAreaWithinTime(startTime, endTime);
        ArrayList<String> list = new ArrayList<>();
        for (ElectricityByArea electricityByArea : electricityByAreaList) {
            list.add(electricityByArea.getArea());
        }
        // 将所有的区域名称与今日的区域名称做筛选,没有补0
        for (String s : areaList) {
            if (!list.contains(s)) {
                ElectricityByArea electricityByArea = new ElectricityByArea();
                electricityByArea.setArea(s);
                electricityByArea.setElectricity(0.0);
                electricityByAreaList.add(electricityByArea);
            }
        }
        return electricityByAreaList;
    }

    @Override
    public List<RunStatusDeviceDetection> getElectricityData() {
        return tElectricityDataMapper.getElectricityData();
    }

    /**
     * 批量删除用电
     *
     * @param ids 需要删除的用电ID
     * @return 结果
     */
    @Override
    public int deleteTElectricityDataByIds(String[] ids) {
        return tElectricityDataMapper.deleteTElectricityDataByIds(ids);
    }

    /**
     * 删除用电信息
     *
     * @param id 用电ID
     * @return 结果
     */
    @Override
    public int deleteTElectricityDataById(String id) {
        return tElectricityDataMapper.deleteTElectricityDataById(id);
    }
}
